(function(){
	 
	var all = $(".all");
	var pay = $(".needpay");
	var shouhuo = $(".shouhuo");
	var pingjia = $(".pingjia");
	var fahuo = $(".fahuo");
	
	sendReq(ctx+"/getOrder",{},function(data){
		var len = data.length;
		if(len == 0){
			all.append("<div style='text-align:center'>您暂无订单</div>")
		}else{
			for(var i=0;i<len;i++){
				var cur = data[i];
				add(cur);
			}
		}
	});
	
	
	function add(data){
		var status = data.status_;
		if(status == -1) return;
		if(data.logistics_number_ == null) data.logistics_number_ = "";
		var titleText = "已完成",btnText = "",logNum = '<p>物流编号：'+data.logistics_number_+'</p>';
		var dom = all;
		if(status == 0){
			titleText = "待付款";
			//<a href="'+ctx+'/address?type=modify&orderid='+data.id+'" class="oncepay">修改收货地址</a>
			btnText = '	<a href="'+ctx+'/pay?id='+data.id+'" class="topay">立即支付</a>	<a href="javascript:cancelOrder(\''+data.id+'\')" class="canelpay">取消订单</a>';
			logNum = '';
			dom = pay;
		}else if(status == 4){
			titleText = "待收货";
			btnText = '<a href="javascript:receive(\''+data.id+'\')" class="topay">确认收货</a>';
			dom = shouhuo;
		}else if(status == 2){
			titleText = "待评价";
			btnText = '<a href="'+ctx+'/orderdetail/'+data.id+'" class="topay">进入评价</a>';
			dom = pingjia;
		}else if(status == 1){//待发货
			titleText = "待发货";
			btnText = '<a href="javascript:quick()" class="topay">催发货</a>';
			logNum = '';
			dom = fahuo;
		}
		
		var html = '<div  order-id = "'+data.id+'"><div class="c-comment">\
			<span class="c-comment-suc" style="border-left:0px;">'+titleText+'</span>\
			</div>\
			<div class="c-comment-list" style="border: 0;">\
				<a href="'+ctx+'/orderdetail/'+data.id+'">\
				<p>应付金额：￥<span>'+data.price_+'</span></p>\
				<p>订单编号：<span style="color:rgb(0,0,204);font-size:14px;text-decoration:underline">'+data.id+'</span></p>\
				<p>下单时间：'+data.create_time_+'</p>'+logNum+'\
	            </a>\
			</div>\
			<div class="c-com-btn">'+btnText+'</div>\
			<div class="clear"></div></div>'
			$html = $(html);
			dom.append($html);
			if(data.status_ != 3){
				all.append($(html));
			}
	}
	
})();
function receive(id){
	hit_box_two('',"",'您确认要收货吗?','take_btn','close_btn_all','确定','取消',function(){
			sendReq(ctx+"/receive/"+id,{},function(data){
					if(data.status == true){
							var doms = $("div[order-id='"+id+"']");
							$(".c-comment-suc",doms).text("待评价")
							$(".c-com-btn a",doms).text("进入评价").attr("href",ctx+"/orderdetail/"+id);
							$(".shouhuo div[order-id='"+id+"']").prependTo($(".pingjia"));
							//location.reload();
					}
			})
	});
}

function quick(){
	hit_box_one('店家已经收到通知啦','','','我知道啦','');
}

function cancelOrder(id){
	hit_box_two('您确认要取消该订单吗?<br>',"优惠券将会退回到您的账户",'','take_btn','close_btn_all','确定','取消',function(){
		sendReq(ctx+"/cancelOrder/"+id,{},function(data){
				if(data.status == true){
						location.reload();
				}
		})
});
}